package drive

import (
	"database/sql"
	"game/config"
	"game/util"
	_ "github.com/go-sql-driver/mysql"
	"strconv"
	"strings"
)

var DB *sql.DB

func InitDB() {
	//构建连接："用户名:密码@tcp(IP:端口)/数据库?charset=utf8"
	path := strings.Join([]string{config.Conn.Mysql.User, ":", config.Conn.Mysql.Pass, "@tcp(", config.Conn.Mysql.Host, ":", strconv.Itoa(config.Conn.Mysql.Port), ")/", config.Conn.Mysql.Select, "?charset=utf8"}, "")
	//打开数据库,前者是驱动名，所以要导入： _ "github.com/go-sql-driver/mysql"
	DB, _ = sql.Open("mysql", path)
	//设置数据库最大连接数
	DB.SetConnMaxLifetime(100)
	//设置上数据库最大闲置连接数
	DB.SetMaxIdleConns(10)
	//验证连接
	if err := DB.Ping(); err != nil {
		util.Instance().PanicFmt("DB链接错误:",err)
	}
}